(function () {
  'use strict';

  angular
    .module('client')
    .directive('upload', upload);

  upload.$inject = ['$stateParams', 'uploadService'];

  function upload($stateParams, uploadService) {
    return {
      restrict: 'E',
      replace: true,
      scope: {
      },
      link: function($scope, element) {
        var $inputBox = $(element[0]).children('.upload-select');
        var $progressBox = $(element[0]).children('.upload-progressbar');
        var $input = $inputBox.children('input[type=file]');
        var $pBar = $progressBox.children('.upload-bar');
        var $progress = $pBar.children('.upload-progress');
        var $text = $progressBox.children('.upload-text');

        $input.on('change', function () {
          uploadService.installFile({
            file: $input.get(0).files[0],
            id:  $stateParams.deviceID,
          }, {
            $iBox: $inputBox,
            $pBox: $progressBox,
            $progress: $progress
          })
            .then(function (res) {
              uploadService.askStatus(res, {
                $iBox: $inputBox,
                $pBox: $progressBox,
                $input: $input,
                $pBar: $pBar,
                $progress: $progress,
                $text: $text
              });
            }, function (err) {
              console.log(err);
            });
        });
      },
      controller: ['$scope', function($scope) {

      }],
      templateUrl: 'app/control/upload/upload.html'
    }
  }


})();
